﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using AimGameLibrary;
using AimGameLibrary.GameObject;

namespace AimGameEngine.EditorObject
{
    enum FlipVerticallyQuadrant
    {
        One,
        Two,
        Three,
        Four,
        None
    }

    public class RectangleSelect : Image2D
    {
        public List<NodeObject> NodeSelect;
        public List<Vector2> LastPosition;
        Rectangle MyBody;

        //Vector2 LastPosition;
        Vector2 NewMove;
        bool Move;
        public RectangleSelect(SpriteBatch inputspritebatch, Camera inputcamera)
            : base(inputspritebatch, inputcamera)
        {
            NodeSelect = new List<NodeObject>();
            LastPosition = new List<Vector2>();
            VisiblE = false;
            MyViewData.Colors = new Color(Color.Green.R,Color.Green.G,Color.Green.B,(byte)50);
            MyViewData.Depth = 0f;
        }

        public String UpdateSize(Vector2 StartPoint, Vector2 EndPoint)
        {
            //StartPoint = StartPoint - camera.MyViewData.Position;
            //EndPoint = EndPoint - camera.MyViewData.Position;
            Rectangle NewRegion = Rectangle.Empty;

            switch (FindQuadrant(StartPoint, EndPoint))
            {
                case FlipVerticallyQuadrant.One:
                    NewRegion = new Rectangle((int)StartPoint.X, (int)StartPoint.Y, (int)(EndPoint.X - StartPoint.X), (int)(EndPoint.Y - StartPoint.Y));
                    break;
                case FlipVerticallyQuadrant.Two:
                    NewRegion = new Rectangle((int)EndPoint.X, (int)StartPoint.Y, (int)(StartPoint.X - EndPoint.X), (int)(EndPoint.Y - StartPoint.Y));
                    break;
                case FlipVerticallyQuadrant.Three:
                    NewRegion = new Rectangle((int)EndPoint.X, (int)EndPoint.Y, (int)(StartPoint.X - EndPoint.X), (int)(StartPoint.Y - EndPoint.Y));
                    break;
                case FlipVerticallyQuadrant.Four:
                    NewRegion = new Rectangle((int)StartPoint.X, (int)EndPoint.Y, (int)(EndPoint.X - StartPoint.X), (int)(StartPoint.Y - EndPoint.Y));
                    break;
            }

            MyViewData.Position = new Vector2(NewRegion.X, NewRegion.Y);
            MyViewData.Source = new Rectangle(0, 0, (int)NewRegion.Width, (int)NewRegion.Height);
            MyBody = NewRegion;
            return NewRegion.ToString() + " " + camera.MyViewData.Position.ToString();

        }

        public void Selection(Dictionary<String, NodeObject> DicNode, Color Select, Color NodeSelcet)
        {
            foreach (String keys in DicNode.Keys)
            {
                if (MyBody.Intersects(DicNode[keys].NodeBody))
                {
                    if(!NodeSelect.Contains(DicNode[keys]))
                        NodeSelect.Add(DicNode[keys]);
                    DicNode[keys].MyViewData.Colors = Select;
                }
                else
                {
                    if (NodeSelect.Contains(DicNode[keys]))
                        NodeSelect.Remove(DicNode[keys]);
                    DicNode[keys].MyViewData.Colors = NodeSelcet;
                }
            }
        }

        public bool DoSelection(Point position, Color NoneSelcet)
        {
            Move = false;
            LastPosition.Clear();

            foreach (NodeObject node in NodeSelect)
            {
                if (node.NodeBody.Intersects(new Rectangle(position.X, position.Y, 1, 1)))
                {
                    Move = true;

                    NewMove = Vector2.Zero;

                    foreach (NodeObject subnode in NodeSelect)
                        LastPosition.Add(subnode.MyViewData.Position);

                    break;
                }
            }

            if (!Move)
            {
                foreach (NodeObject node in NodeSelect)
                    node.MyViewData.Colors = NoneSelcet;
                NodeSelect.Clear();
            }

            return Move;
        }

        public void UpdateMove(Vector2 position)
        {
            for (int i = 0; i < NodeSelect.Count; i++)
            {
                NodeSelect[i].MyViewData.Position = LastPosition[i] + position;
            }
        }

        public void Clean(Color NoneSelect)
        {
            foreach (NodeObject node in NodeSelect)
                node.MyViewData.Colors = NoneSelect;
            NodeSelect.Clear();
        }

        FlipVerticallyQuadrant FindQuadrant(Vector2 StartPoint, Vector2 EndPoint)
        {
            if (StartPoint.X < EndPoint.X && StartPoint.Y < EndPoint.Y)
                return FlipVerticallyQuadrant.One;
            else if (StartPoint.X > EndPoint.X && StartPoint.Y < EndPoint.Y)
                return FlipVerticallyQuadrant.Two;
            else if (StartPoint.X > EndPoint.X && StartPoint.Y > EndPoint.Y)
                return FlipVerticallyQuadrant.Three;
            else if (StartPoint.X < EndPoint.X && StartPoint.Y > EndPoint.Y)
                return FlipVerticallyQuadrant.Four;
            return FlipVerticallyQuadrant.None;
        }

        protected override void SubDraw()
        {
            //base.SubDraw();
            if (mytexture2d != null)
                /// DefultDraw | 1 sprite | Applycamera | SpriteEffect.None | Scale Apply to Position
                spritebatch.Draw(this.mytexture2d
                                        , MyViewData.Position
                                        , MyViewData.Source
                                        , MyViewData.Colors
                                        , MyViewData.Rotation
                                        , MyViewData.Origin
                                        , MyViewData.Scale
                                        , MyViewData.Spriteeffects
                                        , MyViewData.Depth);
        }
    }
}
